<?php
	//=========================
	//Vuong Van Binh
	//Wanbin@vietnambiz.com
	//Preview Message And reply,send
	//=========================
$user_id = intval($_SESSION['adlogin']["id"]);

if($_GET["mode"]){
 
	 $mes_id  = intval($_GET["id"]);
	 
	 $mode_field = "From :";
	  
	 if($_GET["mode"]=="sent"){
	       
		  $mode_field = "To :";
		   
		  $mes_view = $db->query_first("SELECT adm.*,ad.username FROM admin_message as adm INNER JOIN admin as ad ON adm.to_id = ad.id WHERE adm.from_id = ".$user_id." AND adm.mes_id=".$mes_id);
		  
		  if(!$mes_view){echo "<font color=red>Sorry! This message not exist!</font>";}
		  
	 }else{
	 
	 	  $mes_view = $db->query_first("SELECT adm.*,ad.username FROM admin_message as adm INNER JOIN admin as ad ON adm.from_id = ad.id WHERE adm.to_id = ".$user_id." AND adm.mes_id=".$mes_id);
		  
		  if(!$mes_view){echo "<font color=red>Sorry! This message not exist!</font>"; die;}
		  
		  //UPDATE READ FOR MESSAGE
		  $db->exec_update('admin_message',array("`read`"=>"Yes"),'`mes_id`='.$mes_id);
	 
	 }
}else{
  	
	if($_POST){
	
		  $data = array();
		  
		  $data["from_id"] 		= $user_id;
		  $data["to_id"] 		= intval($_POST["to_id"]);
		  $data["title"] 		= strip_tags($_POST["subject"]);
	  	  $data["content"] 		= strip_tags($_POST["message"],"<br>");
		  $data["senddate"] 	= date('Y-m-d h:i:s',time());

		  $insert = $db->exec_insert("admin_message",$data); 

	      if(mysql_affected_rows()>0){
			  echo "<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/active.gif' /><font color='green'><b>Send a messages Successfully!!!!</b></font>"; die();
		  }
	
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Xem trước</title>
</head>

<body bgcolor="#FFFFFF">
<table width="413" height="142" cellpadding="3px;" cellspacing="2px" border="0" style="font-family:tahoma; font-size:12px; border:2px #CCF7F1 solid; background-color:#FFFFFF">
<?php if($_GET["mode"]){ ?> 
  
   <tr>
    <td><strong> <?php echo $mode_field; ?> </strong></td>
    <td><?php echo $mes_view["username"]; ?></td>
  </tr>
  <tr>
    <td width="76"><strong>Tiêu đề:</strong></td>
    <td width="315"><?php echo $mes_view["title"]; ?></td>
  </tr>
  <tr>
    <td><strong>Ngày: </strong></td>
    <td><?php echo date('d/m/Y , h:i:s',strtotime($mes_view['senddate']));?></td>
  </tr>
 
  <tr>
    <td valign="top"><strong>Tin nhắn:</strong></td>
    
	<td valign="top">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2" valign="top"><div style="float:left; min-height:110px; height:auto; text-align:justify"><?php echo nl2br($mes_view["content"]); ?></div></td>
  </tr>
  
		  <?php if($_GET["mode"]=="sent"){ ?>
		  <tr>
			<td colspan="2" valign="top">&nbsp;</td>
		  </tr>
		  <tr>
			<td colspan="2" valign="top" align="center"><a href="<?php echo ADMIN_URL; ?>/index.php?module=message-preview&reply_id=<?php echo $mes_view["to_id"];?>&reply_name=<?php echo $mes_view["username"]; ?>" style="text-decoration:none"><font color="#0066CC">Kích để gửi tin nhắn tới <strong><?php echo $mes_view["username"]; ?></strong></font></a></td>
		  </tr>
		  <?php }else if($_GET["mode"]=="inbox"){ ?>
		  
		  <tr>
			<td colspan="2" valign="top">&nbsp;</td>
		  </tr>
		  <tr>
			<td colspan="2" valign="top" align="center"><a href="<?php echo ADMIN_URL; ?>/index.php?module=message-preview&reply_id=<?php echo $mes_view["from_id"];?>&reply_name=<?php echo $mes_view["username"]; ?>" style="text-decoration:none"><font color="#0066CC">Kích để gửi tin nhắn tới <strong><?php echo $mes_view["username"]; ?></strong></font></a></td>
		  </tr>
		   <?php } ?>
		   
<?php }else{ ?>  
  <form action="" method="post">
  
  <tr>
    <td width="76" align="right"><strong>Tới:</strong></td>
    <td width="315"><?php echo $_GET["reply_name"]; ?><input type="hidden" value="<?php echo intval($_GET["reply_id"]); ?>" name="to_id" /></td>
  </tr>
  <tr>
    <td width="76" align="right"><strong>Tiêu đề:</strong></td>
    <td width="315"><input type="text" name="subject" style="width:315px;" id="subject"/></td>
  </tr>
  <tr>
    <td width="76" valign="top" align="right"><strong>Nội dung:</strong></td>
    <td width="315" valign="top"><textarea name="message" cols="37" rows="7" id="message"></textarea></td>
  </tr>
  
   <tr>
    <td width="76" valign="top"><strong></strong></td>
    <td width="315" valign="top">
	<input type="submit" name="send" value="Send" onclick="return check_send();" />
	<input type="button" name="back" value="Back" onclick="javascript:history.back();" />	</td>
  </tr>
  </form>
<?php } ?> 
</table>
<script type="text/javascript">
function check_send()
{
	if(document.getElementById('subject').value==''){
	 	alert("Please type subject!"); document.getElementById('subject').focus(); return false;
	}else if(document.getElementById('message').value==''){
		alert("Please type message!"); document.getElementById('message').focus(); return false;
	}
	return true;
}
</script>
</body>
</html>
